package xxy.com.customview.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;

/**
 * Created by Administrator on 2017/12/6.
 */

public class MyScrollView extends ScrollView {
    private InterceptHeight mInterceptHeight;
    int lastY = 0;
    int lastX = 0;
    int lastY_touch = 0;
    int lastX_touch = 0;
    private boolean isIntercept;
    private RequestFocus mRequestFocus;

    public MyScrollView(Context context) {
        super(context);
        init(context);
    }


    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
    }

    /**
     * 是否拦截
     * @param ev
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean intercept = false;
        int x = (int) ev.getX();
        int y = (int) ev.getY();

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                intercept = false;
                break;
            case MotionEvent.ACTION_MOVE:
                intercept = isIntercept;   //在Main5Activity里根据setInterceptHeight(InterceptHeight interceptHeight)回调传回来的值
//                intercept = true;   //子view内部拦截scrollview的标记
                Log.e("onInterceptTouchEvent", "ACTION_MOVE" );
                break;
            case MotionEvent.ACTION_UP:
                Log.e("onInterceptTouchEvent", "ACTION_UP" );
                intercept = true;
                break;
            default:
                break;
        }
        lastY = y;
        lastX = x;
        return intercept;
    }

    /**
     * 触摸时做的逻辑
     * @param ev
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int x = (int) ev.getX();
        int y = (int) ev.getY();

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaY = y - lastY_touch;
                int deltaX = x - lastX_touch;
                smoothScrollBy(-deltaX, -deltaY);   //平缓滑动
                Log.e("onTouchEvent", "ACTION_MOVE" );
                if (!isIntercept) {   //scrollview滑动到大于图片的y值时，Main5Activity里根据setInterceptHeight(InterceptHeight interceptHeight)回调会传回false
                    setScrollY(525);   //设置scrollview的y值为一个定值，scrollview就不动了
//                    clearFocus();
                    this.mRequestFocus.requestFocus();
                }
                break;
            case MotionEvent.ACTION_UP:
                Log.e("onTouchEvent", "ACTION_UP" );
                break;
            default:
                break;
        }
        lastY_touch = y;
        lastX_touch = x;
        return true;
    }

    /**
     * scrollview滑动时的 x 和 y 的值的改变
     * @param x
     * @param y
     * @param oldx
     * @param oldy
     */
    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if (mInterceptHeight != null) {
            isIntercept = mInterceptHeight.mInterceptHeight(x, y, oldx, oldy);   //调用接口里的方法
        }

//        Log.e("onScrollChanged", " getHeight():\t" +  getHeight() + "\ngetChildAt(0).getMeasuredHeight():\t" + getChildAt(0).getMeasuredHeight());
    }

    public interface InterceptHeight {
        public boolean mInterceptHeight(int x, int y, int oldx, int oldy);
    }

    public void setInterceptHeight(InterceptHeight interceptHeight) {
        this.mInterceptHeight = interceptHeight;
    }

    public interface RequestFocus {
        void requestFocus();
    }

    public void setFoucus(RequestFocus requestFocus) {
        this.mRequestFocus = requestFocus;
    }
}
